/**
 * Copyright(c) 2013 Jade Techonologies Co., Ltd.
 *
 * History:
 *   13-4-26 上午10:51 Created by dwzhang
 */
package com.jade.framework.startup.tomcat;

import java.io.File;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import com.jade.framework.base.JadeSystem;
import org.apache.catalina.startup.Bootstrap;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;

/**
 * 启动tomcat, TomcatBootstrap必须放置在容器中。
 *
 * @author dwzhang
 * @version 1.0 13-4-26 上午10:51
 */
public class TomcatBootstrap implements Runnable
{
    private String catalinaBase;
    private String catalinaHome;
    private String logConfigFile;
    private String tmpDir;
    private String jaasConf;
    private Log log = LogFactory.getLog(TomcatBootstrap.class);

    public void setCatalinaBase(String catalinaBase)
    {
        this.catalinaBase = catalinaBase;
    }

    public void setCatalinaHome(String catalinaHome)
    {
        this.catalinaHome = catalinaHome;
    }

    public void setLogConfigFile(String logConfigFile)
    {
        this.logConfigFile = logConfigFile;
    }

    public void setTmpDir(String tmpDir)
    {
        this.tmpDir = tmpDir;
    }

    public String getCatalinaBase()
    {
        return catalinaBase;
    }

    public String getCatalinaHome()
    {
        return catalinaHome;
    }

    public String getLogConfigFile()
    {
        return logConfigFile;
    }

    public String getTmpDir()
    {
        return tmpDir;
    }

    public String getJaasConf()
    {
        return jaasConf;
    }

    public void setJaasConf(String jaasConf)
    {
        this.jaasConf = jaasConf;
    }

    @PostConstruct
    public void init()
    {
        //需要启动一个线程来启动tomcat
        Thread thread = new Thread(this);
        thread.start();
    }

    public void run()
    {
        try {
            String defaultPath = new File(System.getProperty("JADE_HOME", "/jade"), "tomcat").getPath();
            if (StringUtils.isBlank(catalinaBase)) {
                catalinaBase = defaultPath;
            }
            if (StringUtils.isBlank(catalinaHome)) {
                catalinaHome = defaultPath;
            }
            //        if (StringUtils.isBlank(tmpDir)) {
            //            tmpDir = defaultPath;
            //        }
            //if (StringUtils.isBlank(logConfigFile)) {
            //    logConfigFile = "org.apache.juli.ClassLoaderLogManager";
            //}
            if (!StringUtils.isBlank(jaasConf)) {
                System.setProperty("java.security.auth.login.config", jaasConf);
            }

            System.setProperty("catalina.base", catalinaBase);
            System.setProperty("catalina.home", catalinaHome);
            if (StringUtils.isNotBlank(tmpDir)) {
                System.setProperty("java.io.tmpdir", tmpDir);
            }

            if (StringUtils.isNotBlank(logConfigFile)) {
                System.setProperty("java.util.logging.manager", logConfigFile);
            }

            String args[] = {"start"};

            JadeSystem.getLog().info("Start tomcat...base:" + catalinaBase + " home:" + catalinaHome);
            Bootstrap.main(args);
            JadeSystem.getLog().info("Tomcat started");
        }
        catch (Throwable t) {
            log.error("Can't boot tomcat instance:", t);
        }
    }

    @PreDestroy
    public void destroy()
    {
        String args[] = {"stop"};
        Bootstrap.main(args);
    }
}
